﻿using System;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.Common.Area;
using BCService.Common.Area;
using BCTMSRESTful.Configs;
using BCCommon.FreightEnums;
using System.Collections.Generic;
using BCService.Utils;
using System.Net;

namespace BCTMSRESTful.Controllers.Management.Area
{
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class CitiesController : ManagementBaseController
    {
        private readonly ICityService cityService;

        public CitiesController(ICityService cityService)
        {
            this.cityService = cityService;
        }
      
        /// <summary>
        /// 获取市信息
        /// </summary>
        /// <param name="cityCode">市Id</param>
        /// <returns></returns>
        [HttpGet("{city_code}")]
        [ProducesResponseType(typeof(CityDto), 200)]
        public IActionResult Get([FromRoute(Name ="city_code")]string cityCode)
        {
            return Ok(cityService.Get(cityCode));
        }

        /// <summary>
        /// 获取市列表
        /// </summary>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <param name="provinceCode">省Id</param>
        /// <param name="cityCode">市Id</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<CityDto>), 200)]
        public IActionResult Get([FromQuery(Name = "province_code")]string provinceCode,
            [FromQuery(Name = "city_code")]string cityCode, 
            [FromQuery(Name = "page_index")]int index = 1, 
            [FromQuery(Name = "page_size")]int size = 10)
        {
            return Ok(cityService.Get(provinceCode, cityCode, index, size));
        }
      
        /// <summary>
        /// 添加市信息
        /// </summary>
        /// <param name="dto">市信息模型</param>
        /// <returns></returns>
        [HttpPost]
        [Log("添加城市信息")]
        [ProducesResponseType(typeof(CityDto), 200)]
        public IActionResult Post([FromBody]CityRequestDto dto)
        {
            if (cityService.Add(dto,this.Admin))
            {
                return StatusCode((int)HttpStatusCode.Created, cityService.Get(dto.CityCode));
            }
            else {
                return BadRequest("添加失败");
            }
        }

        /// <summary>
        /// 修改市信息
        /// </summary>
        /// <param name="cityId">市Id</param>
        /// <param name="dto">市信息模型</param>
        /// <returns></returns>
        [HttpPut("{city_id}")]
        [Log("修改市信息")]
        [ProducesResponseType(typeof(CityDto), 200)]
        public IActionResult Put([FromRoute(Name ="city_id")]long cityId, [FromBody]CityRequestDto dto)
        {
            CityPutDto putDto = dto.As<CityPutDto>();
            putDto.CityId = cityId;
            cityService.Update(putDto,this.Admin);
            return StatusCode((int)HttpStatusCode.OK, this.cityService.Get(cityId));
        }
    
        /// <summary>
        /// 删除市信息
        /// </summary>
        /// <param name="cityCode">市Id</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpDelete("{city_code}")]
        [Log("删除市信息")]
        public IActionResult Delete([FromRoute(Name = "city_code")]string cityCode)
        {
            cityService.Delete(cityCode);
            return NoContent();
        }

        /// <summary>
        /// 修改城市需要议价
        /// </summary>
        /// <param name="cityCode">市Id</param>
        /// <param name="businessType"></param>
        /// <param name="isNegotiatedPrice">需要议价</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpPut("[action]/{city_code}")]
        [Log("修改城市议价状态")]
        public IActionResult NegotiatedPrice([FromRoute(Name = "city_code")]string cityCode,
            [FromQuery(Name = "business_type")]FreightBusinessType businessType,
            [FromQuery(Name = "is_negotiated_price")]bool isNegotiatedPrice)
        {
            cityService.UpdateCityNegotiatedPrice(cityCode, businessType, isNegotiatedPrice);
            return this.NoContent();            
        }
    }
}
